﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace SQLBackup
{
    internal static class Utility
    {
        /// <summary>
        /// To Check Null State of the object 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool CheckNullState(object obj)
        {
            return (obj != null && obj != DBNull.Value);
        }

        /// <summary>
        /// This method will write all the settings that the service has in xml file
        /// </summary>
        /// <param name="settings"> instance of settings object</param>
        /// <param name="strXmlFilePath">path for writing file</param>
        public static void WriteSettingInXml(ConfigSettings settings, string strXmlFilePath)
        {
            // load the service configuration for this service. 
            var xmlFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
                                      strXmlFilePath);

            using (var fsConfigFile = new FileStream(xmlFilePath, FileMode.Create, FileAccess.Write))
            {
                using (var swConfigWriter = new StreamWriter(fsConfigFile))
                {
                    var serializer = new XmlSerializer(typeof(ConfigSettings));
                    serializer.Serialize(swConfigWriter, settings);
                }
            }
        }

        /// <summary>
        /// Reading setting from the xml file and return the ConfiSettings class object
        /// </summary>
        /// <param name="configFilePath">config file path</param>
        /// <returns></returns>
        internal static ConfigSettings ReadSettingsFromXml(string configFilePath)
        {
            ConfigSettings configInfo = null;

            //read all the config data to start the service
            using (var reader = new StreamReader(configFilePath))
            {
                var serializer = new XmlSerializer(typeof(ConfigSettings));
                configInfo = (ConfigSettings)serializer.Deserialize(reader);
            }
            return configInfo;
        }

        /// <summary>
        /// Log message to text file
        /// </summary>
        /// <param name="message"></param>
        public static void LogErrorToFile(string message)
        {
            
           var logFilePath = Path.Combine(Config.ActivityLog, "BackUpLog.txt");
           LogMessage(message, logFilePath);
        }

        public static void LogMessageToFile(string message)
        {
           var logFilePath = Path.Combine(Config.ActivityLog, "ActivityLog.txt");
           LogMessage(message, logFilePath);
        }

        public static void LogMessage(string message, string filePath)
        {
            try
            {
                message = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + Environment.NewLine +
                            message + Environment.NewLine +
                          "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + Environment.NewLine;

                using (var fs = new FileStream(filePath, FileMode.Append, FileAccess.Write))
                {
                    using (var writeToLog = new StreamWriter(fs))
                    {
                        writeToLog.WriteLine(message);
                    }
                }
            }
            catch (IOException ioexcpt)
            {
                var evntlog = new EventLog { Source = "AutoDBBackUp" };
                evntlog.WriteEntry(ioexcpt + message);
            }
        }
    }

    internal class Constant
    {
        public const string K_FULL_BACKUP_TYPE = "Full";
        public const string K_DIFFERENTIAL_BACKUP_TYPE = "Differential";
    }
}
